package com.odianyun.hello.service.impl;
import com.odianyun.hello.Utils.IdUtils;
import com.odianyun.hello.model.PO.Article;
import com.odianyun.hello.model.PO.ArticleExtends.CommentInfo;
import com.odianyun.hello.model.PO.ArticleExtends.LikeInfo;
import com.odianyun.hello.service.IArticleService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class ArticleServiceImpl implements IArticleService {

    IdUtils idUtils = new IdUtils();
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private RedisTemplate redisTemplate;

    Criteria criteria = new Criteria();
    @Override
    public String addArticle(Article article) {
        article.setCreateTime(new Date());
        mongoTemplate.insert(article);
        return "success";
    }

    @Override
    public List<Article> findArticleByTitleRegex(String title) {
        Pattern pattern = Pattern.compile("^.*" + title + ".*$", Pattern.CASE_INSENSITIVE);
        criteria.and("title").regex(pattern);
        Query query = new Query(criteria);
        List<Article> articles = mongoTemplate.find(query, Article.class);

        return articles;
    }

    @Override
    public void deleteArticleById(String id) {
        //criteria.and("title").in(title);
        Query query = new Query(Criteria.where("_id").is(id));
        mongoTemplate.remove(query,Article.class);
    }

    @Override
    public List<Article> getArticleByTitle(String title) {
        Query query = new Query(Criteria.where("title").is(title));
        Update update = new Update();
        update.inc("viewNum");
        mongoTemplate.updateFirst(query, update, Article.class);
        List<Article> articles = mongoTemplate.find(query,Article.class);
        return articles;
    }

    @Override
    public void addComment(String id , CommentInfo commentInfos) {
        Query query = new Query(Criteria.where("_id").is(id));
        commentInfos.setId(idUtils.getRentId());
        commentInfos.setUserId(idUtils.getRentId());
        Update update = new Update();
        update.push("commentInfo", commentInfos);
        mongoTemplate.updateFirst(query, update, Article.class);
    }

    @Override
    public String addLike(String id, LikeInfo likeInfos) {
        Query query = new Query(Criteria.where("_id").is(id));
        likeInfos.setId(idUtils.getRentId());
        //likeInfos.setUserId(idUtils.getRentId());
        String userId=likeInfos.getUserId();
        if(redisTemplate.opsForValue().get("likeInfos"+userId)!=null){
            return "请不要重复点赞";
        }
        Update update = new Update();
        update.push("likeInfo", likeInfos);
        mongoTemplate.updateFirst(query, update, Article.class);
        redisTemplate.opsForValue().set("likeInfos"+userId,"已点赞");
        return "点赞成功";
    }

    @Override
    public void deleteLike(String title, String id) {
        Query query = new Query(Criteria.where("title").is(title));
        Update update = new Update();
        LikeInfo likeinfo = new LikeInfo();
        likeinfo.setId(id);
        update.pull("likeInfo", likeinfo);
        mongoTemplate.updateFirst(query, update, Article.class);
    }

    @Override
    public void deleteComment(String title, String id) {
        Query query = new Query(Criteria.where("title").is(title));
        Update update = new Update();
        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setId(id);
        update.pull("commentInfo",commentInfo);
        mongoTemplate.updateFirst(query, update, Article.class);
    }
}
